package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.*;
import com.cloud.bc.model.request.LikeRequest;
import com.cloud.bc.model.request.PhotoReq;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.model.response.UserGiftResponse;
import com.cloud.bc.service.*;
import com.cloud.common.model.enums.CurrencyTypeEnum;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/gift")
public class Ajax_GiftController {

    private final BcGiftsService bcGiftsService;
    private final BcUsersService bcUsersService;
    private final BcLikesService bcLikesService;
    private final BcUserGiftsService bcUserGiftsService;


    @GetMapping("list")
    public AjaxResp list(PhotoReq req, HttpServletRequest request,
                         HttpServletResponse response) {
        AjaxResp ajaxResp = new AjaxResp();
        List<BcGifts> bcGifts = bcGiftsService.selectAll(null);
        ajaxResp.setList(bcGifts);
        return ajaxResp;
    }

    /**
     * 赠送礼物
     */
    @GetMapping("give")
    public AjaxResp give(HttpServletRequest request,
                         HttpServletResponse response,
                         int userId,
                         int giftId
    ) {
        return bcUserGiftsService.userGift(request, userId, giftId);
    }



    /**
     * 喜欢
     */
    @GetMapping("like")
    public AjaxResp greetSend(HttpServletRequest request,
                              HttpServletResponse response,
                              BcLikes req
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        //判断是否登录
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (req.getTargetId().equals(userInfo.getId())) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("好自恋啊，不要太喜欢自己");
            return ajaxResp;
        }
        //不喜欢
        if (req.getLikeType().equals("unlike")) {
            //删除
            QueryWrapper<BcLikes> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(BcLikes::getUserId, userInfo.getId());
            queryWrapper.lambda().eq(BcLikes::getTargetId, req.getTargetId());
            bcLikesService.remove(queryWrapper);
        } else {
            //喜欢
            //添加
            BcLikes bcLikes = new BcLikes();
            bcLikes.setTargetId(req.getTargetId());
            bcLikes.setUserId(userInfo.getId());
            bcLikesService.save(bcLikes);
        }
        return ajaxResp;
    }

    //我收到的礼物
    @GetMapping("getReceiveGift")
    public AjaxResp getReceiveGift(HttpServletRequest request, HttpServletResponse response,
                                   LikeRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        //判断是否登录
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        Page p = new Page();
        p.setCurrent(Convert.toInt(req.getPage(), 0) <= 0 ? 1 : req.getPage());

        List<UserGiftResponse> list = bcUserGiftsService.getReceiveGift(p, userInfo.getId());
        ajaxResp.setList(list);
        return ajaxResp;
    }

    //我送出的 礼物
    @GetMapping("getGiveGift")
    public AjaxResp getGiveGift(HttpServletRequest request, HttpServletResponse response,
                                LikeRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        //判断是否登录
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        Page p = new Page();
        p.setCurrent(Convert.toInt(req.getPage(), 0) <= 0 ? 1 : req.getPage());
        List<UserGiftResponse> list = bcUserGiftsService.getGiveGift(p, userInfo.getId());
        ajaxResp.setList(list);
        return ajaxResp;
    }
}
